package com.finfosoft.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;

public class Server {

//	public static final String HOST = "tcp://121.42.253.149:61713";
//    public static final String TOPIC = "oldDtu";
//    public static final String TOPIC125 = "toclient/125";
//    private static final String clientid = "test";

//    private String userName = "admin";
//    private String passWord = "finfosoft123";
	public MqttClient client;
    public  MqttTopic topic;
    
    public MqttMessage message;

    Prop prop = PropKit.use("config.properties");
    
    
    public  Server(String topic,String serverid) throws MqttException {
        // MemoryPersistence设置clientid的保存形式，默认为以内存保存
        client = new MqttClient(prop.get("mqtt.host"), serverid, new MemoryPersistence());
        connect(topic);
    }

    private void connect(String mqttTopic) {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
        options.setUserName(prop.get("mqtt.username"));
        options.setPassword(prop.get("mqtt.password").toCharArray());
        // 设置超时时间
        options.setConnectionTimeout(10);
        // 设置会话心跳时间
        options.setKeepAliveInterval(20);
        try {
            client.setCallback(new PushCallback());
            client.connect(options);
            topic = client.getTopic(mqttTopic);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    
    public void publish(MqttTopic topic , MqttMessage message) throws MqttPersistenceException, MqttException {
    		MqttDeliveryToken token = topic.publish(message);
    		token.waitForCompletion();
    		//System.out.println("message is published completely! "+ token.isComplete());
    }
    
    
    
//    public static void main(String[] args) throws MqttException {
//        Server server = new Server();
//        for (int i = 0; i < 1000000; i++) {
//        	try {
//    			Thread.sleep(20000);
//    		} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//    			e.printStackTrace();
//    		}
//       	 	server.message = new MqttMessage();
//            server.message.setQos(1);
//            server.message.setRetained(true);
//            //server.message.setPayload("[{'data_id':123456,'real_range_low':0},{'data_id':123456,'real_range_low':0},{'data_id':123456,'real_range_low':0}]".getBytes());
//            server.message.setPayload(("[{'data_id':123456"+i+",'real_range_low':0,'real_range_high':0},{'data_id':123456,'real_range_low':0}]").getBytes());
//            server.publish(server.topic , server.message);
//            System.out.println(server.message.isRetained() + "------ratained状态");
//		}
//
//    }
//    
    
    
}
